package com.toweave.starter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.toweave.starter.R;
import com.toweave.starter.beans.CityBean;

import java.util.List;

public class CityBaseAdapter extends BaseAdapter {
  private Context ctx;
  private List<CityBean> list;

  public CityBaseAdapter(Context ctx, List<CityBean> list) {
    this.ctx = ctx;
    this.list = list;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public Object getItem(int position) {
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      // 根据布局文件实例化view
      convertView = LayoutInflater.from(ctx).inflate(R.layout.item_list, null);
      holder = new ViewHolder();
      holder.iv_item_icon = convertView.findViewById(R.id.iv_item_icon);
      holder.tv_item_name = convertView.findViewById(R.id.tv_item_name);
      holder.tv_item_desc = convertView.findViewById(R.id.tv_item_desc);
      // 将视图持有者保存到view中
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    // 给控件设置数据
    CityBean city = list.get(position);
    holder.iv_item_icon.setImageResource(city.icon);
    holder.tv_item_name.setText(city.name);
    holder.tv_item_desc.setText(city.desc);

    return convertView;
  }

  public static class ViewHolder {
    ImageView iv_item_icon;
    TextView tv_item_name;
    TextView tv_item_desc;
  }
}
